<?php

/**
 * This is the model class for table "{{members}}".
 *
 * The followings are the available columns in table '{{members}}':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $register_time
 * @property string $register_ip
 * @property string $last_visit_time
 * @property string $last_login_ip
 * @property string $login_times
 * @property integer $status
 */
class Members extends CActiveRecord {

    public $re_password;
    public $verifyCode;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Members the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{members}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username', 'required', 'message' => '帐号不能为空'),
            array('username', 'length', 'min' => 5, 'max' => 18, 'tooShort' => '帐号长度：5-18位', 'tooLong' => '帐号长度：5-18位'),
            array('username', 'unique', 'message' => '帐号已存在', 'on' => 'register'),
            array('password', 'required', 'message' => '密码不能为空'),
            array('password', 'length', 'min' => 5, 'max' => 18, 'tooShort' => '密码长度：5-18位', 'tooLong' => '密码长度：5-18位'),
            array('re_password', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码输入不一致'),
            array('verifyCode', 'required', 'message' => '验证码不能为空'),
            array('verifyCode', 'captcha', 'message' => '验证码输入错误'),
            array('login_times,status', 'safe')
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'register_time' => 'Register Time',
            'register_ip' => 'Register Ip',
            'last_visit_time' => 'Last Visit Time',
            'last_login_ip' => 'Last Login Ip',
            'login_times' => 'Login Times',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('register_time', $this->register_time, true);
        $criteria->compare('register_ip', $this->register_ip, true);
        $criteria->compare('last_visit_time', $this->last_visit_time, true);
        $criteria->compare('last_login_ip', $this->last_login_ip, true);
        $criteria->compare('login_times', $this->login_times, true);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    function beforeSave() {
        if ($this->isNewRecord) {
            $time = time();
            $ip = Yii::app()->request->userHostAddress;
            $this->register_time = $time;
            $this->register_ip = $ip;
            $this->last_visit_time = $time;
            $this->last_login_ip = $ip;
        }
        return parent::beforeSave();
    }

}